<!DOCTYPE html>


<html lang="en">
  

    <head>
      <meta charset="utf-8" />
        
      <meta name="description" content="時光的个人博客" />
      
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
	  <meta name="referrer" content="no-referrer" />

      <title>遍历List移除元素时踩坑点 |  時光</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      
<link rel="stylesheet" href="/css/fonts/remixicon.css">

      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.staticfile.org/pace/1.2.4/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    <link rel="alternate" href="/atom.xml" title="時光" type="application/atom+xml">
</head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-遍历List移除元素时踩坑点"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  遍历List移除元素时踩坑点
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/11/20/%E9%81%8D%E5%8E%86List%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0%E6%97%B6%E8%B8%A9%E5%9D%91%E7%82%B9/" class="article-date">
  <time datetime="2023-11-20T04:37:02.000Z" itemprop="datePublished">2023-11-20</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E8%B8%A9%E5%9D%91/">踩坑</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">1.3k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">5 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="1、问题描述"><a href="#1、问题描述" class="headerlink" title="1、问题描述"></a>1、问题描述</h1><p>在遍历List并在循环体中移除元素时需要注意以下几点</p>
<ol>
<li><p>移除元素后数据总量会越来越小，可能造成数组下标越界</p>
</li>
<li><p>移除元素后，每个元素原有位置也会发生改变，需确认移除的元素是否是真正需要移除的</p>
</li>
<li><p>由于删除元素后，每个元素位置前移，会有部分数据直接跳过循环</p>
<p>例如 数组中有以下数据</p>
<table>
<thead>
<tr>
<th>i</th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
</tr>
</thead>
<tbody><tr>
<td>val</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</tbody></table>
<p>当 i &#x3D; 1 时 移除元素2，3会前移，下标1的值变为3，下标2的值变为4，后面的元素依次前移</p>
<p>当 i &#x3D; 1 的循环体结束后，i 自增，进入 i &#x3D; 2 的循环体，此时 i &#x3D; 2 对应的值为4 ，3被跳过，不经过循环体</p>
</li>
</ol>
<h1 id="2、问题复现"><a href="#2、问题复现" class="headerlink" title="2、问题复现"></a>2、问题复现</h1><p>假设我有一个数组，需要移除下标为 0，1，2，3 的元素</p>
<p><img src="https://img2023.cnblogs.com/blog/2233039/202311/2233039-20231120125314494-287185037.png" alt="image-20231120125313864"></p>
<p>代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">   <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testRemove1</span><span class="params">()</span>&#123;</span><br><span class="line">       List&lt;Integer&gt; numbers = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">       <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">           numbers.add(i);</span><br><span class="line">       &#125;</span><br><span class="line">       System.out.println(<span class="string">&quot;old_numbers:&quot;</span>+numbers);</span><br><span class="line"></span><br><span class="line">       List&lt;Integer&gt; itemsToRemove = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">       itemsToRemove.add(<span class="number">0</span>);</span><br><span class="line">       itemsToRemove.add(<span class="number">1</span>);</span><br><span class="line">       itemsToRemove.add(<span class="number">2</span>);</span><br><span class="line">       itemsToRemove.add(<span class="number">3</span>);</span><br><span class="line">       System.out.println(itemsToRemove);</span><br><span class="line"></span><br><span class="line">       <span class="comment">//移除元素时数据总长度减小,元素原本位置改变</span></span><br><span class="line">       <span class="comment">// 不仅会造成数组下标越界，移除的元素也并非想要移除的元素</span></span><br><span class="line">       <span class="keyword">for</span> (<span class="type">int</span>  integer : itemsToRemove) &#123;</span><br><span class="line">           numbers.remove(integer);</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       System.out.println(<span class="string">&quot;new_numbers:&quot;</span>+numbers);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<p>假设我只需要移除下标为 0，1 的元素，下标0 对应的值应为0，下标1 对应的值应为1</p>
<p>移除后的元素应该是 [2，3，4] 但结果却是 [1，3，4]</p>
<p><img src="https://img2023.cnblogs.com/blog/2233039/202311/2233039-20231120125731060-518159466.png" alt="image-20231120125730760"></p>
<h1 id="3、解决方案"><a href="#3、解决方案" class="headerlink" title="3、解决方案"></a>3、解决方案</h1><p>可以使用 <code>ListIterator</code> 来安全地在迭代过程中删除元素，而不影响后续元素的位置</p>
<p><code>ListIterator</code> 是 Java 中 <code>List</code> 接口提供的一个迭代器，相比普通的迭代器，它具有双向遍历的能力，允许在迭代过程中插入、删除元素，以及获取当前位置的索引。这使得在使用 <code>List</code> 类型的集合时，特别是在需要在迭代过程中修改集合的情况下，<code>ListIterator</code> 是一个更加强大的工具。</p>
<p><img src="https://img2023.cnblogs.com/blog/2233039/202311/2233039-20231120130456248-1856484750.png" alt="image-20231120130455982"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testRemove2</span><span class="params">()</span>&#123;</span><br><span class="line">        List&lt;Integer&gt; numbers = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">            numbers.add(i);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;old_numbers:&quot;</span>+numbers);</span><br><span class="line"></span><br><span class="line">        List&lt;Integer&gt; itemsToRemove = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        itemsToRemove.add(<span class="number">0</span>);</span><br><span class="line">        itemsToRemove.add(<span class="number">1</span>);</span><br><span class="line">        itemsToRemove.add(<span class="number">2</span>);</span><br><span class="line">        itemsToRemove.add(<span class="number">3</span>);</span><br><span class="line">        System.out.println(itemsToRemove);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 使用 ListIterator 正序遍历并删除需要删除的元素</span></span><br><span class="line">        ListIterator&lt;Integer&gt; iterator = numbers.listIterator();</span><br><span class="line">        <span class="keyword">while</span> (iterator.hasNext()) &#123;</span><br><span class="line">            <span class="type">int</span> <span class="variable">indexToRemove</span> <span class="operator">=</span> iterator.nextIndex();</span><br><span class="line">            <span class="type">Integer</span> <span class="variable">nextVal</span> <span class="operator">=</span> iterator.next();<span class="comment">// 移动到下一个位置</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//            System.out.println(&quot;index:&quot;+indexToRemove+&quot;,value:&quot;+nextVal);</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (itemsToRemove.contains(nextVal))&#123;</span><br><span class="line">                iterator.remove(); <span class="comment">// 安全地移除当前元素</span></span><br><span class="line">                System.out.println(<span class="string">&quot;移除元素 index:&quot;</span>+indexToRemove+<span class="string">&quot;,value:&quot;</span>+nextVal);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;new_numbers:&quot;</span>+numbers);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>如果需要根据下标移除元素可以逆序遍历，此时<code>itemsToRemove.contains(indexToRemove)</code>使用的是下标</p>
<p><img src="https://img2023.cnblogs.com/blog/2233039/202311/2233039-20231120134702755-245472491.png" alt="image-20231120134702313"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testRemove3</span><span class="params">()</span>&#123;</span><br><span class="line">        List&lt;Integer&gt; numbers = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; <span class="number">5</span>; i++) &#123;</span><br><span class="line">            numbers.add(i);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;old_numbers:&quot;</span>+numbers);</span><br><span class="line"></span><br><span class="line">        List&lt;Integer&gt; itemsToRemove = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        itemsToRemove.add(<span class="number">0</span>);</span><br><span class="line">        itemsToRemove.add(<span class="number">1</span>);</span><br><span class="line">        itemsToRemove.add(<span class="number">2</span>);</span><br><span class="line">        itemsToRemove.add(<span class="number">3</span>);</span><br><span class="line">        System.out.println(itemsToRemove);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 使用 ListIterator 逆序遍历并删除需要删除的元素</span></span><br><span class="line">        ListIterator&lt;Integer&gt; iterator = numbers.listIterator(numbers.size());</span><br><span class="line">        <span class="keyword">while</span> (iterator.hasPrevious()) &#123;</span><br><span class="line">            <span class="type">int</span> <span class="variable">indexToRemove</span> <span class="operator">=</span> iterator.previousIndex();</span><br><span class="line">            <span class="type">Integer</span> <span class="variable">previousVal</span> <span class="operator">=</span> iterator.previous();<span class="comment">// 移动到上一个位置</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (itemsToRemove.contains(indexToRemove))&#123;</span><br><span class="line">                iterator.remove(); <span class="comment">// 安全地移除当前元素</span></span><br><span class="line">                System.out.println(<span class="string">&quot;移除元素 index:&quot;</span>+indexToRemove+<span class="string">&quot;,value:&quot;</span>+previousVal);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;new_numbers:&quot;</span>+numbers);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>根据下标移除元素时正序遍历可能会出问题</p>
<p><img src="https://img2023.cnblogs.com/blog/2233039/202311/2233039-20231120140148055-1669205910.png" alt="image-20231120140147752"></p>
<h2 id="ListIterator常见方法"><a href="#ListIterator常见方法" class="headerlink" title="ListIterator常见方法"></a>ListIterator常见方法</h2><ul>
<li><code>hasNext()</code>：判断是否还有下一个元素。</li>
<li><code>hasPrevious()</code>：判断是否还有上一个元素。</li>
<li><code>next()</code>：返回下一个元素。</li>
<li><code>previous()</code>：返回上一个元素。</li>
<li><code>nextIndex()</code>：返回下一个元素的索引。</li>
<li><code>previousIndex()</code>：返回上一个元素的索引。</li>
<li><code>add(E element)</code>：在当前位置插入指定的元素。</li>
<li><code>set(E element)</code>：用指定的元素替换上一次调用 <code>next()</code> 或 <code>previous()</code> 返回的元素。</li>
<li><code>remove()</code>：移除上一次调用 <code>next()</code> 或 <code>previous()</code> 返回的元素。</li>
</ul>
<h2 id="ListIterator-两种初始化方式"><a href="#ListIterator-两种初始化方式" class="headerlink" title="ListIterator 两种初始化方式"></a>ListIterator 两种初始化方式</h2><p><code>ListIterator</code> 有两个不同的初始化方式，分别是：</p>
<ol>
<li><code>ListIterator&lt;Integer&gt; iterator = numbers.listIterator()</code>：通过这种方式初始化的 <code>ListIterator</code> 位于列表的开始位置，即索引为 0 的位置。当你使用 <code>iterator.next()</code> 时，它会返回列表的第一个元素。</li>
<li><code>ListIterator&lt;Integer&gt; iterator = numbers.listIterator(numbers.size())</code>：通过这种方式初始化的 <code>ListIterator</code> 位于列表的末尾位置，即索引为 <code>list.size()</code> 的位置。当你使用 <code>iterator.previous()</code> 时，它会返回列表的最后一个元素。</li>
</ol>
<p>总的来说，这两种方式初始化的 <code>ListIterator</code> 分别定位在列表的开始和末尾位置，选择取决于你是希望从前向后遍历还是从后向前遍历。</p>
<p>示例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">javaCopy codeList&lt;String&gt; myList = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;(Arrays.asList(<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>, <span class="string">&quot;C&quot;</span>, <span class="string">&quot;D&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 从前向后遍历</span></span><br><span class="line">ListIterator&lt;String&gt; iteratorForward = myList.listIterator();</span><br><span class="line"><span class="keyword">while</span> (iteratorForward.hasNext()) &#123;</span><br><span class="line">    System.out.print(iteratorForward.next() + <span class="string">&quot; &quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出: A B C D</span></span><br><span class="line"></span><br><span class="line">System.out.println();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 从后向前遍历</span></span><br><span class="line">ListIterator&lt;String&gt; iteratorBackward = myList.listIterator(myList.size());</span><br><span class="line"><span class="keyword">while</span> (iteratorBackward.hasPrevious()) &#123;</span><br><span class="line">    System.out.print(iteratorBackward.previous() + <span class="string">&quot; &quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出: D C B A</span></span><br></pre></td></tr></table></figure>

<p>在这个示例中，<code>iteratorForward</code> 从前向后遍历列表，而 <code>iteratorBackward</code> 从后向前遍历列表。</p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          Donate
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>Copyright： </strong>
          
          Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2023/11/20/%E9%81%8D%E5%8E%86List%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0%E6%97%B6%E8%B8%A9%E5%9D%91%E7%82%B9/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/List/" rel="tag">List</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2023/11/20/JAVA%E5%88%86%E6%89%B9%E5%A4%84%E7%90%86%E6%95%B0%E6%8D%AE%E7%AE%80%E5%8D%95%E7%A4%BA%E4%BE%8B/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            JAVA分批处理数据简单示例
          
        </div>
      </a>
    
    
      <a href="/2023/11/16/Git%E5%9B%A0%E6%96%87%E4%BB%B6%E8%BF%87%E5%A4%A7%E5%AF%BC%E8%87%B4%E6%8F%90%E4%BA%A4%E5%A4%B1%E8%B4%A5%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Git因文件过大导致提交失败解决方案</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.staticfile.org/valine/1.4.16/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "dx3B54wOwhi75vxC2IAZagtg-gzGzoHsz",
    app_key: "m4k2fhnFa1iPI4vbofHqMY5X",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
  
   
    <script src="https://cdn.staticfile.org/twikoo/1.4.18/twikoo.all.min.js"></script>
    <div id="twikoo" class="twikoo"></div>
    <script>
        twikoo.init({
            envId: ""
        })
    </script>
 
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2023
        <i class="ri-heart-fill heart_icon"></i> Shiguang
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="時光"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.staticfile.org/justifiedGallery/3.8.1/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.staticfile.org/photoswipe/4.1.3/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.staticfile.org/photoswipe/4.1.3/default-skin/default-skin.min.css">
<script src="https://cdn.staticfile.org/photoswipe/4.1.3/photoswipe.min.js"></script>
<script src="https://cdn.staticfile.org/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.staticfile.org/clipboard.js/2.0.10/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>